﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;
using AccountingLibs;

namespace ListForms
{
    public partial class FrmEditHangHoa : ListForms.FrmBaseEditForm
    {
        public FrmEditHangHoa(VatTuHangHoa VatTuHangHoa)
        {
            InitializeComponent();
            _DoiTuong = VatTuHangHoa;

            BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtChietKhau, BasicForms.TextFormatType.Integer);
        }

        public FrmEditHangHoa(string ten)
        {
            InitializeComponent();
            InitializeComponent();
            _DoiTuong = null;
            txtTen.EditValue = ten;

            BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
            BasicForms.FrmBase.FormatTextEdit(txtChietKhau, BasicForms.TextFormatType.Integer);
        }

        protected override void FillData()
        {
            cmbNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Filter("LienQuanDenKho = 1").DataView;
            cmbNhomVatTuHangHoa.DisplayColumn = "Ten";
            cmbNhomVatTuHangHoa.ValueColumn = "ID";
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ten", "Tên");

            if (_DoiTuong == null)
            {
                this.Text = "Thêm mới vật tư hàng hóa - dịch vụ";
                _DoiTuong = DanhMucVatTuHangHoa.Instance.NewDoiTuong();
                txtGiaMua.EditValue = (decimal)0;
                txtGiaBan.EditValue = (decimal)0;
                txtGiaBia.EditValue = (decimal)0;
                txtChietKhau.EditValue = (int)0;
                txtNhaXuatBan.EditValue = "";
                txtNguonCungCap.EditValue = "";
                txtTacGia.EditValue = "";
                _EditState = EditState.CreatingNew;
            }
            else
            {
                this.Text = "Sửa thông tin vật tư hàng hóa - dịch vụ";
                txtTen.Text = (_DoiTuong as VatTuHangHoa).Ten;
                txtNhaXuatBan.Text = (_DoiTuong as VatTuHangHoa).NhaXuatBan;
                txtNguonCungCap.Text = (_DoiTuong as VatTuHangHoa).NguonCungCap;
                txtTacGia.Text = (_DoiTuong as VatTuHangHoa).TacGia;
                txtChietKhau.EditValue = (_DoiTuong as VatTuHangHoa).ChietKhau;
                chkSuDung.Checked = (_DoiTuong as VatTuHangHoa).TrangThai;
                chkKhongSuDung.Checked = !(chkSuDung.Checked);
                cmbNhomVatTuHangHoa.EditValue = (_DoiTuong as VatTuHangHoa).NhomVatTuHangHoa.ID;
                txtGiaBan.EditValue = (_DoiTuong as VatTuHangHoa).GiaBanHienTai;
                txtGiaMua.EditValue = (_DoiTuong as VatTuHangHoa).GiaMuaHienTai;
                txtGiaBia.EditValue = (_DoiTuong as VatTuHangHoa).GiaBia;

                txtMa.Text = (_DoiTuong as VatTuHangHoa).Ma;

                txtDVTinh.Text = (_DoiTuong as VatTuHangHoa).DonViTinh;
                _EditState = EditState.Modifying;

                txtMa.Enabled = !VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID);
            }
        }
        protected override bool CheckFullInfo()
        {
            if (txtMa.Text == "") throw new CustomException("Chưa điền mã vật tư hàng hóa - dịch vụ");
            if (txtTen.Text == "") throw new CustomException("Chưa điền tên vật tư hàng hóa - dịch vụ");
            if (cmbNhomVatTuHangHoa.EditValue == null) throw new CustomException("Chưa chọn nhóm vật tư hàng hóa - dịch vụ");
            if (txtGiaBan.EditValue == null) throw new CustomException("Chưa nhập giá bán mặc định");
            if (txtGiaMua.EditValue == null) throw new CustomException("Chưa nhập giá mua mặc định");
            if (txtGiaBia.EditValue == null) throw new CustomException("Chưa nhập giá bìa mặc định");

            if (_EditState == EditState.Modifying && VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID)
                                                    && txtMa.Text != (_DoiTuong as VatTuHangHoa).Ma)
            {
                throw new CustomException("Không được sửa mã của hàng đã được giao dịch");
            }
            return true;
        }

        protected override void SetDoiTuongProperties()
        {
            (_DoiTuong as VatTuHangHoa).Ma = txtMa.Text.Trim();
            (_DoiTuong as VatTuHangHoa).Ten = txtTen.Text.Trim();
            (_DoiTuong as VatTuHangHoa).TrangThai = chkSuDung.Checked;
            (_DoiTuong as VatTuHangHoa).TacGia = txtTacGia.Text.Trim();
            (_DoiTuong as VatTuHangHoa).NguonCungCap = txtNguonCungCap.Text.Trim();
            (_DoiTuong as VatTuHangHoa).NhaXuatBan = txtNhaXuatBan.Text.Trim();
            (_DoiTuong as VatTuHangHoa).ChietKhau = (int)txtChietKhau.EditValue;
            (_DoiTuong as VatTuHangHoa).NhomVatTuHangHoa = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
            (_DoiTuong as VatTuHangHoa).GiaBanHienTai = (decimal)txtGiaBan.EditValue;
            (_DoiTuong as VatTuHangHoa).GiaMuaHienTai = (decimal)txtGiaMua.EditValue;
            (_DoiTuong as VatTuHangHoa).GiaBia = (decimal)txtGiaBia.EditValue;
            (_DoiTuong as VatTuHangHoa).DonViTinh = txtDVTinh.Text.Trim();
        }

        private void chkSuDung_Click(object sender, EventArgs e)
        {
            chkKhongSuDung.Checked = false; ;
        }

        private void chkKhongSuDung_CheckedChanged(object sender, EventArgs e)
        {
            chkSuDung.Checked = false;
        }


        private void txtGiaBan_EditValueChanged(object sender, EventArgs e)
        {
            txtGiaBia.EditValue = txtGiaBan.EditValue;
        }

        private void MakeBarCode()
        {
            if (txtMa.Text == "")
                txtMa.Text = DanhMucVatTuHangHoa.GenBarCode((Guid)cmbNhomVatTuHangHoa.EditValue);
        }

        private void cmbNhomVatTuHangHoa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhomVatTuHangHoa.EditValue is Guid)
                {
                    NhomVatTuHangHoa nhom = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                    if (nhom != null)
                    {
                        if (nhom.IsSach)
                        {
                            lblTitle.Text = "Sách";
                            lblGiaBia.Text = "Giá bìa";
                            txtTacGia.Properties.ReadOnly = false;
                            txtNhaXuatBan.Properties.ReadOnly = false;
                        }
                        else
                        {
                            lblTitle.Text = "Hàng hóa khác sách";
                            lblGiaBia.Text = "Giá tại cửa hàng";
                            txtNhaXuatBan.Text = "";
                            txtNhaXuatBan.Text = "";
                            txtTacGia.Properties.ReadOnly = true;
                            txtNhaXuatBan.Properties.ReadOnly = true;
                        }
                    }
                    MakeBarCode();
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
    }
}
